﻿using System;
using System.Collections.Generic;
using KKSG;
using XMainClient.UI;
using XMainClient.UI.UICommon;
using XUpdater;
using XUtliPoolLib;

namespace XMainClient
{
	// Token: 0x02000967 RID: 2407
	internal class XRadioDocument : XDocComponent
	{
		// Token: 0x17002C57 RID: 11351
		// (get) Token: 0x06009105 RID: 37125 RVA: 0x0014B4AC File Offset: 0x001496AC
		public override uint ID
		{
			get
			{
				return XRadioDocument.uuID;
			}
		}

		// Token: 0x06009106 RID: 37126 RVA: 0x000FEEFC File Offset: 0x000FD0FC
		protected override void OnReconnected(XReconnectedEventArgs arg)
		{
		}

		// Token: 0x06009107 RID: 37127 RVA: 0x0014B4C4 File Offset: 0x001496C4
		public override void Update(float fDeltaT)
		{
			base.Update(fDeltaT);
			bool flag = this.startLoop;
			if (flag)
			{
				this.acc_time += fDeltaT;
				this.all_time += fDeltaT;
				bool flag2 = this.all_time > (float)this.TimeOut && !this.mJoinBigRoomSucc;
				if (flag2)
				{
					this.startLoop = false;
					this.mJoinBigRoomSucc = false;
					this.roomState = XRadioDocument.BigRoomState.OutRoom;
					DlgBase<RadioDlg, RadioBehaviour>.singleton.Refresh(false);
					XSingleton<XDebug>.singleton.AddGreenLog("join room timeout, Apollo error!", null, null, null, null, null);
					XSingleton<UiUtility>.singleton.ShowSystemTip(XSingleton<XStringTable>.singleton.GetString("FM_ENTER_OVERTIME"), "fece00");
				}
				bool flag3 = this.acc_time > 0.4f;
				if (flag3)
				{
					bool flag4 = !this.mJoinBigRoomSucc;
					if (flag4)
					{
						IApolloManager xapolloManager = XSingleton<XUpdater.XUpdater>.singleton.XApolloManager;
						bool joinRoomBigResult = xapolloManager.GetJoinRoomBigResult();
						if (joinRoomBigResult)
						{
							this.startLoop = false;
							this.mJoinBigRoomSucc = true;
							xapolloManager.openMusic = true;
							xapolloManager.openSpeak = false;
							this.roomState = XRadioDocument.BigRoomState.InRoom;
							this.MuteSounds(false);
							DlgBase<RadioDlg, RadioBehaviour>.singleton.Refresh(true);
							DlgBase<RadioDlg, RadioBehaviour>.singleton.UpdateHostInfo();
							XSingleton<UiUtility>.singleton.ShowSystemTip(XStringDefineProxy.GetString("FM_ENTER_SUCCESS"), "fece00");
							XSingleton<XChatIFlyMgr>.singleton.StopAutoPlay();
							XSingleton<XChatIFlyMgr>.singleton.EnableAutoPlay(false);
						}
					}
					this.acc_time = 0f;
				}
			}
		}

		// Token: 0x06009108 RID: 37128 RVA: 0x0014B640 File Offset: 0x00149840
		public override void OnDetachFromHost()
		{
			this.cacheRadioOpen = false;
			this.QuitApolloBigRoom();
			base.OnDetachFromHost();
		}

		// Token: 0x06009109 RID: 37129 RVA: 0x0014B658 File Offset: 0x00149858
		public override void OnEnterScene()
		{
			base.OnEnterScene();
			bool flag = XSingleton<XGame>.singleton.CurrentStage.Stage == EXStage.World;
			if (flag)
			{
				XChatDocument specificDocument = XDocuments.GetSpecificDocument<XChatDocument>(XChatDocument.uuID);
				bool flag2 = specificDocument.GetBattleRaw().real > 0;
				if (flag2)
				{
					bool flag3 = this.roomState == XRadioDocument.BigRoomState.InRoom;
					if (flag3)
					{
						this.cacheRadioOpen = true;
					}
					this.QuitBigRoom();
				}
			}
			else
			{
				bool flag4 = XSingleton<XGame>.singleton.CurrentStage.Stage == EXStage.Hall;
				if (flag4)
				{
					bool flag5 = this.cacheRadioOpen;
					if (flag5)
					{
						bool flag6 = this.roomState == XRadioDocument.BigRoomState.OutRoom;
						if (flag6)
						{
							this.JoinBigRoom();
							this.cacheRadioOpen = false;
						}
					}
				}
			}
		}

		// Token: 0x0600910A RID: 37130 RVA: 0x0014B70C File Offset: 0x0014990C
		public void UpdateHost(List<string> names, List<ulong> roles)
		{
			bool flag = names != null;
			if (flag)
			{
				this.hostlist = names;
				this.hostIDs = roles;
			}
			bool flag2 = DlgBase<RadioDlg, RadioBehaviour>.singleton.IsVisible();
			if (flag2)
			{
				DlgBase<RadioDlg, RadioBehaviour>.singleton.UpdateHostInfo();
			}
		}

		// Token: 0x0600910B RID: 37131 RVA: 0x0014B750 File Offset: 0x00149950
		public void MuteSounds(bool mute)
		{
			bool flag = this.roomState == XRadioDocument.BigRoomState.InRoom;
			if (flag)
			{
				IApolloManager xapolloManager = XSingleton<XUpdater.XUpdater>.singleton.XApolloManager;
				if (mute)
				{
					xapolloManager.SetMusicVolum(0);
				}
				else
				{
					XOptionsDocument specificDocument = XDocuments.GetSpecificDocument<XOptionsDocument>(XOptionsDocument.uuID);
					int @int = XSingleton<XGlobalConfig>.singleton.GetInt("SetSpeakerVolume");
					xapolloManager.SetMusicVolum((int)((float)@int * specificDocument.voiceVolme));
				}
			}
		}

		// Token: 0x0600910C RID: 37132 RVA: 0x0014B7BC File Offset: 0x001499BC
		public void JoinBigRoom()
		{
			bool flag = !this.mJoinBigRoomSucc && !this.startLoop;
			if (flag)
			{
				this.roomState = XRadioDocument.BigRoomState.Processing;
				XSingleton<UiUtility>.singleton.ShowSystemTip(XStringDefineProxy.GetString("FM_ENTERING"), "fece00");
				RpcC2M_JoinFmRoom rpc = new RpcC2M_JoinFmRoom();
				XSingleton<XClientNetwork>.singleton.Send(rpc);
			}
		}

		// Token: 0x0600910D RID: 37133 RVA: 0x0014B818 File Offset: 0x00149A18
		public void ProcessJoinBigRoom(JoinLargeRoomReply reply)
		{
			this.acc_time = 0f;
			this.all_time = 0f;
			this.startLoop = true;
			this.mJoinBigRoomSucc = false;
			XSingleton<XDebug>.singleton.AddLog("url:", reply.url, " bussinessid:" + reply.bussniessid, " role:" + reply.key, null, null, XDebugColor.XDebug_None);
			XSingleton<XDebug>.singleton.AddLog("roomid: ", reply.roomid.ToString(), " roomkey:", reply.roomkey.ToString(), null, null, XDebugColor.XDebug_None);
			IApolloManager xapolloManager = XSingleton<XUpdater.XUpdater>.singleton.XApolloManager;
			xapolloManager.SetRealtimeMode();
			xapolloManager.JoinBigRoom(reply.url, (int)reply.key, reply.bussniessid, (long)reply.roomid, (long)reply.roomkey, (short)reply.memberid);
		}

		// Token: 0x0600910E RID: 37134 RVA: 0x0014B8FF File Offset: 0x00149AFF
		public void ProcessTimeOut()
		{
			this.mJoinBigRoomSucc = false;
			this.roomState = XRadioDocument.BigRoomState.OutRoom;
			DlgBase<RadioDlg, RadioBehaviour>.singleton.Refresh(false);
		}

		// Token: 0x0600910F RID: 37135 RVA: 0x0014B91C File Offset: 0x00149B1C
		public void QuitBigRoom()
		{
			this.QuitApolloBigRoom();
			PtcC2M_LeaveLargeRoom proto = new PtcC2M_LeaveLargeRoom();
			XSingleton<XClientNetwork>.singleton.Send(proto);
		}

		// Token: 0x06009110 RID: 37136 RVA: 0x0014B944 File Offset: 0x00149B44
		private void QuitApolloBigRoom()
		{
			try
			{
				this.startLoop = false;
				this.mJoinBigRoomSucc = false;
				bool flag = this.roomState == XRadioDocument.BigRoomState.InRoom;
				if (flag)
				{
					IApolloManager xapolloManager = XSingleton<XUpdater.XUpdater>.singleton.XApolloManager;
					bool flag2 = xapolloManager != null;
					if (flag2)
					{
						xapolloManager.openSpeak = false;
						xapolloManager.QuitBigRoom();
					}
				}
				this.roomState = XRadioDocument.BigRoomState.OutRoom;
				bool flag3 = XSingleton<XGame>.singleton.CurrentStage.Stage == EXStage.Hall;
				if (flag3)
				{
					XSingleton<XChatIFlyMgr>.singleton.EnableAutoPlay(true);
					XSingleton<XChatIFlyMgr>.singleton.StartAutoPlay(true, true);
				}
			}
			catch (Exception ex)
			{
				XSingleton<XDebug>.singleton.AddLog(ex.StackTrace.ToString(), null, null, null, null, null, XDebugColor.XDebug_None);
			}
		}

		// Token: 0x06009111 RID: 37137 RVA: 0x0014BA04 File Offset: 0x00149C04
		public bool isHosting()
		{
			IApolloManager xapolloManager = XSingleton<XUpdater.XUpdater>.singleton.XApolloManager;
			return this.roomState != XRadioDocument.BigRoomState.OutRoom && this.isHost && xapolloManager.openSpeak;
		}

		// Token: 0x04003010 RID: 12304
		public new static readonly uint uuID = XSingleton<XCommon>.singleton.XHash("XRadioDocument");

		// Token: 0x04003011 RID: 12305
		public List<string> hostlist = new List<string>();

		// Token: 0x04003012 RID: 12306
		public List<ulong> hostIDs = new List<ulong>();

		// Token: 0x04003013 RID: 12307
		private bool mJoinBigRoomSucc = false;

		// Token: 0x04003014 RID: 12308
		public bool isHost = false;

		// Token: 0x04003015 RID: 12309
		public XRadioDocument.BigRoomState roomState = XRadioDocument.BigRoomState.OutRoom;

		// Token: 0x04003016 RID: 12310
		private bool startLoop = false;

		// Token: 0x04003017 RID: 12311
		private float acc_time = 0f;

		// Token: 0x04003018 RID: 12312
		private float all_time = 0f;

		// Token: 0x04003019 RID: 12313
		private int TimeOut = 8;

		// Token: 0x0400301A RID: 12314
		private bool cacheRadioOpen = false;

		// Token: 0x02001963 RID: 6499
		public enum BigRoomState
		{
			// Token: 0x04007E01 RID: 32257
			OutRoom,
			// Token: 0x04007E02 RID: 32258
			Processing,
			// Token: 0x04007E03 RID: 32259
			InRoom
		}
	}
}
